package com.shzu.jwc.ty.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shzu.jwc.ty.bean.JudgeProblem;
import com.shzu.jwc.ty.bean.Msg;
import com.shzu.jwc.ty.bean.ProblemClass;
import com.shzu.jwc.ty.service.JudgeProblemservice;
import com.shzu.jwc.ty.service.ProblemClassService;

@Controller
@RequestMapping("judge")
public class JudgeProblemcontroller {

	@Autowired
	JudgeProblemservice judgeservice;
	
	@Autowired
	ProblemClassService problemclassservice;
	
	@RequestMapping("/jugeproblem.action")
	public String judgeproblem(@RequestParam(value="pn",defaultValue="1") Integer pn,Model model)
	{
		PageHelper.startPage(pn,10);
		List<JudgeProblem> judgedata=judgeservice.getjugeproblem();
		List<ProblemClass> classdata=problemclassservice.getproblemclass();
		PageInfo pageinfo=new PageInfo(judgedata,10);
		model.addAttribute("pageinfo", pageinfo);
		model.addAttribute("classdata",classdata);
		return "/judge/judgeproblem";
	}
	
	@ResponseBody
	@RequestMapping("/uploadjudge.action")
	public Msg uploadjudge(@RequestParam("file") MultipartFile file) throws Exception
	{
		if(file.isEmpty())
			return Msg.error("题库不能为空");
		List<String> list=new ArrayList<String>();
		Integer error=0;
		String errormessge="";
		Integer statue=null;
		JudgeProblem judgeProblem=new JudgeProblem();
		String[] a=new String[3];
		BufferedReader br=new BufferedReader(new InputStreamReader(file.getInputStream(),"GBK"));
		String stemp;
		br.readLine();
		while((stemp = br.readLine()) != null)
		{
			list.add(stemp);
		}
		for(Integer i=0;i<list.size();i++)
		{
			a=list.get(i).split(",");
			judgeProblem.setJudgeProblemTitle(a[0]);
			judgeProblem.setJudgeProblemRight(Integer.parseInt(a[1]));
			statue=problemclassservice.valuegetid(a[2]);
			if(statue==404) {
				error++;
				errormessge=errormessge+(i+2)+" ";
				continue;
			}
			judgeProblem.setProblemClassId(problemclassservice.valuegetid(a[2]));
			judgeservice.insertjudge(judgeProblem);
		}
		if(error==0)
			return Msg.success("导入成功");
		else
			return Msg.success("导入成功，共"+error+"条错误，错误为第"+errormessge+"行");
	}
	
	@ResponseBody
	@RequestMapping("/insertjudge.action")
	public Msg insertjudge(JudgeProblem judgeproblem)
	{
		if(judgeservice.insertjudge(judgeproblem)>0)
			return Msg.success("添加成功");
		else
			return Msg.error("添加失败");
	}
	
	@ResponseBody
	@RequestMapping("/deletejudge.action")
	public Msg deletejudge(@RequestParam("id") Integer judgeProblemId)
	{
		if(judgeservice.deletejudge(judgeProblemId)>0)
			return Msg.success("删除成功");
		else
			return Msg.error("删除失败");
	}
	
	@ResponseBody
	@RequestMapping("/updatejudge.action")
	public Msg updatejudge(@RequestParam("id") Integer id,@RequestParam("column") String column,@RequestParam("value") String value)
	{
		if(judgeservice.updatejudge(id, column, value)>0)
			return Msg.success("修改成功");
		else
			return Msg.error("修改失败");
	}
	
	@RequestMapping("/searchjudge.action")
	public String searchjudge(@RequestParam(value="pn",defaultValue="1") Integer pn,@RequestParam("problemClassId") Integer problemClassId,Model model,HttpSession session)
	{
		List<ProblemClass> classdata=problemclassservice.getproblemclass();
		if(problemClassId==0) {
			PageHelper.startPage(pn,10);
			List<JudgeProblem> judgedata=judgeservice.getjugeproblem();
			PageInfo pageinfo=new PageInfo(judgedata,10);
			model.addAttribute("pageinfo", pageinfo);
		}
		else
		{
			PageHelper.startPage(pn,10);
			List<JudgeProblem> judgedata=judgeservice.getsearchjugeproblem(problemClassId);
			PageInfo pageinfo=new PageInfo(judgedata,10);
			model.addAttribute("pageinfo", pageinfo);
			session.setAttribute("problemClassId", problemClassId);
		}
		model.addAttribute("classdata",classdata);
		return "/judge/judgeproblem";
		
	}
}
